package com.pinecone.service.impl;

import com.pinecone.dao.CusPrincipalDao;
import com.pinecone.dao.UtilRandomSlaveDao;
import com.pinecone.dto.HostAndPrincipalDTO;
import com.pinecone.entity.CusPrincipalEntity;
import com.pinecone.entity.UtilRandomSlaveEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.pinecone.dao.CusHostDao;
import com.pinecone.entity.CusHostEntity;
import com.pinecone.service.CusHostService;



@Service("cusHostService")
public class
CusHostServiceImpl implements CusHostService {
	@Autowired
	private CusHostDao cusHostDao;
    @Autowired
    private UtilRandomSlaveDao utilRandomSlaveDao;
    @Autowired
    private CusPrincipalDao cusPrincipalDao;
	@Override
	public CusHostEntity queryObject(Long hostId){
		return cusHostDao.queryObject(hostId);
	}
	
	@Override
	public List<CusHostEntity> queryList(Map<String, Object> map){
		return cusHostDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return cusHostDao.queryTotal(map);
	}
	
	@Override
	public void save(CusHostEntity cusHost){
		cusHostDao.save(cusHost);
	}
	
	@Override
	public void update(CusHostEntity cusHost){
		cusHostDao.update(cusHost);
	}
	
	@Override
	public void delete(Long hostId){
		cusHostDao.delete(hostId);
	}
	
	@Override
	public void deleteBatch(Long[] hostIds){
		cusHostDao.deleteBatch(hostIds);
	}

    /**
     * 机构管理员添加主播
     * @param hostEntity
     * @param masterId
     */
    @Override
    public void addHost(CusHostEntity hostEntity, Integer masterId) {
        //生成随机有序账号
        UtilRandomSlaveEntity randomSlaveEntity = generateRandomSlave(masterId);
        //生成主播登录账号密码
        CusPrincipalEntity principalEntity = saveSlaveUser(hostEntity.getHostPhone()
                ,randomSlaveEntity.getSlaveAccount().toString(),hostEntity.getOrgId());
        //保存主播信息
        hostEntity.setCustomerId(principalEntity.getCusId());
        cusHostDao.save(hostEntity);
    }
    //生成随机账户工具
    public UtilRandomSlaveEntity generateRandomSlave(Integer userId){
        UtilRandomSlaveEntity randomSlaveEntity = new UtilRandomSlaveEntity();
        randomSlaveEntity.setMasterId(userId);
        //记录当前子帐号状态，当子帐号删除时候需要更改状态
        randomSlaveEntity.setHostState(true);
        utilRandomSlaveDao.save(randomSlaveEntity);
        return randomSlaveEntity;
    }
    //保存子帐号(将用户注册是的手机号后6位作为登录密码)
    public CusPrincipalEntity saveSlaveUser(String phone, String username, Integer orgId){
        CusPrincipalEntity slaveEntity = new CusPrincipalEntity();
        final String rawPassword = phone.substring(
                phone.length()-6,phone.length());

        slaveEntity.setCusAccount(username);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        slaveEntity.setCusPassword(encoder.encode(rawPassword));
        //设置默认属性
        slaveEntity.setState(true)
                .setLastPasswordResetDate(new Date())
                .setParent(false)
                .setOrgId(orgId);
        cusPrincipalDao.save(slaveEntity);
        return slaveEntity;
    }

    @Override
    public CusHostEntity queryByCusId(Integer cusId) {
        return cusHostDao.queryByCusId(cusId);
    }

    @Override
    public int queryCombineTotal(Map<String, Object> map) {
        return cusHostDao.queryCombineTotal(map);
    }

    @Override
    public List<HostAndPrincipalDTO> queryCombineList(Map<String, Object> map) {
        return cusHostDao.queryCombineList(map);
    }

    @Override
    public List<HostAndPrincipalDTO> queryCombineListByOrgId(Integer orgId) {
        return cusHostDao.queryCombineListByOrgId(orgId);
    }

    @Override
    public CusHostEntity queryByLiveRoomId(String liveRoomId) {
        return cusHostDao.queryByLiveRoomId(liveRoomId);
    }

}
